import { create, all, bignumber } from 'mathjs';

const mathjs = create(all, {
    number: 'BigNumber', // 设置默认数值类型为BigNumber
    precision: 20, // 可选：设置BigNumber的默认精度
});

const { add, subtract, multiply, divide } = mathjs;

const quitOperate = (args, operator) => {
    // 至少有一个Number,并且其他参数通过Number转换后是合法的number值
    const hasOneNumber = args.every((argItem) => {
        argItem = Number(argItem);
        return !isNaN(argItem);
    });

    // 必须全部是 Number类型才能计算
    const allIsNumber = args.every((argItem) => {
        return typeof argItem === 'number';
    });

    //  + 的操作符
    if (operator === '+') {
        if (!allIsNumber) {
            //退出计算
            return {
                result: true,
            };
        }
    } else if (!hasOneNumber) {
        //退出计算
        return {
            result: true,
        };
    }

    return {
        result: false,
    };
};

export const accAdd = (...args) => {
    //字符串连接
    const quitObj = quitOperate(args, '+');
    if (quitObj.result) {
        return eval('args[0]+args[1]');
    } else {
        try {
            args = args.map((item) => bignumber(item));
            return Number(add(...args).toString());
        } catch (e) {
            return eval('args[0]+args[1]');
        }
    }
};

export const accSub = (...args) => {
    const quitObj = quitOperate(args, '-');
    if (quitObj.result) {
        return eval('args[0]-args[1]');
    } else {
        try {
            args = args.map((item) => bignumber(item));
            return Number(subtract(args[0], args[1]).toString());
        } catch (e) {
            console.log(`args = ${JSON.stringify([...args])}-- accSub--err`);
            return eval('args[0]-args[1]');
        }
    }
};
// 乘
export const accMul = (...args) => {
    const quitObj = quitOperate(args, '*');
    if (quitObj.result) {
        return eval('args[0]*args[1]');
    } else {
        try {
            args = args.map((item) => bignumber(item));
            return Number(multiply(...args).toString());
        } catch (e) {
            console.log(`args = ${JSON.stringify([...args])}-- accMul`);
            return eval('args[0]*args[1]');
        }
    }
};
// 除
export const accDiv = (...args) => {
    const quitObj = quitOperate(args, '/');
    if (quitObj.result) {
        return eval('args[0]/args[1]');
    } else {
        try {
            args = args.map((item) => bignumber(item));
            return Number(divide(args[0], args[1]).toString());
        } catch (e) {
            console.log(`args = ${JSON.stringify([...args])}-- accDiv--err`);
            return eval('args[0]/args[1]');
        }
    }
};

window.accAdd = accAdd;
window.accSub = accSub;
window.accMul = accMul;
window.accDiv = accDiv;

export default {
    accAdd,
    accSub,
    accMul,
    accDiv,
    add: accAdd,
    subtract: accSub,
    multiply: accMul,
    divide: accDiv,
};

export { accAdd as add };
export { accSub as subtract };
export { accMul as multiply };
export { accDiv as divide };
